package com.compass.mobile.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentCaseItemBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.Nurse
import com.compass.mobile.presenters.NurseItemPresenter
import com.compass.mobile.ui.activity.CaseDetailsActivity
import com.compass.mobile.ui.viewModel.NurseProjectViewModel
import com.compass.mobile.widget.MyBridgeAdapter


/**
 * Description： 护理ItemFragment
 * Date:2024/5/21 11:13
 */
class CaseItemFragment : BaseVBFragment<FragmentCaseItemBinding>() {

    private val nurseProjectViewModel: NurseProjectViewModel by viewModels()
    private var nurses: MutableList<Nurse> = mutableListOf()
    override fun initView(view: View, savedInstanceState: Bundle?) {



    }

    fun setSelect(id: String) {
        showLoading()
        nurseProjectViewModel.getMoreNurse(id)

    }

    override fun initData() {
        nurseProjectViewModel.nurseLiveData.observe(this) {
            it?.let { list ->
                setNurseData(list)
            }
            dismissLoading()
        }

    }


    private fun setNurseData(nurseList: MutableList<Nurse>) {
        nurses = nurseList
        if (nurseList.isNullOrEmpty()){
            mBinding?.emptyView?.visible()
        }else{
            mBinding?.emptyView?.gone()

        }
        initSpecialColumn()
    }

    override fun intListener() {
        mBinding?.tvPrevious?.onClick {
            showToast("上一页")
        }

        mBinding?.tvNextPage?.onClick {
            showToast("下一页")
        }
    }


    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
            val item=nurses[position]



            item?.let {
                CaseDetailsActivity.start(requireContext(),it.id, it.classifyId)

            }




        }
    }


    private fun initSpecialColumn() {
        mBinding?.caseItem?.post {
            // 使用获取到的宽度
            val width = mBinding?.caseItem?.width
            width?.let {
                specialColumn(it)
            }
        }
    }

    @SuppressLint("RestrictedApi")
    fun specialColumn(width: Int) {
        val finalWidth = width - dpToPx(200)

        mBinding?.caseItem?.setNumColumns(3)
        mBinding?.caseItem?.horizontalSpacing = dpToPx(10)
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)

        val caseItemPresenter =
            NurseItemPresenter()
        caseItemPresenter.setFinalWidth(finalWidth / 3)
        val arrayObjectAdapter = ArrayObjectAdapter(caseItemPresenter)
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )
        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_SMALL, false
        )
        arrayObjectAdapter.addAll(0, nurses)

    }


}